package com.ego.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.ego.product.po.Brand;
import com.ego.product.vo.BrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ego.product.po.CategoryBrandRelation;
import com.ego.product.service.CategoryBrandRelationService;
import com.ego.common.util.Result;

/**
 * @author KamTang
 * @program: ego
 * @description 品牌分类关联控制器
 * @date 2022-11-13 12:56:14
 */
@RestController
@RequestMapping("product/category-brand-relation")
public class CategoryBrandRelationController {

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 根据brandId获取品牌分类关联
     */
    @GetMapping("/get")
    public Result getCategory(@RequestParam("brandId") Long brandId) {
        List<CategoryBrandRelation> data = categoryBrandRelationService.get(brandId);
        return Result.ok().put("data", data);
    }

    /**
     * 根据分类ID获取品牌
     */
    @GetMapping("/brand")
    public Result getBrand(@RequestParam("categoryId") Long categoryId) {
        List<Brand> brands = categoryBrandRelationService.getByCatId(categoryId);
        List<BrandVo> list = brands.stream().map(Brand::toVo).collect(Collectors.toList());
        return Result.ok().put("list", list);
    }

    /**
     * 信息
     */
    @GetMapping("/get/{id}")
    public Result get(@PathVariable("id") Long id) {
        CategoryBrandRelation categoryBrandRelation = categoryBrandRelationService.getById(id);
        return Result.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public Result save(@RequestBody CategoryBrandRelation categoryBrandRelation) {
        categoryBrandRelationService.saveCategoryBrandRelation(categoryBrandRelation);
        return Result.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public Result update(@RequestBody CategoryBrandRelation categoryBrandRelation) {
        categoryBrandRelationService.updateById(categoryBrandRelation);
        return Result.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody Long[] ids) {
        categoryBrandRelationService.removeByIds(Arrays.asList(ids));
        return Result.ok();
    }

}
